using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Dynamic;
using YTS.Tools;

namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0137. 只出现一次的数字(其余均出现三次)")]
    public class No0137_SingleNumber2 : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(SingleNumber, new int[] { 2, 2, 3, 2 }, 3);
            Assert.TestExe(SingleNumber, new int[] { 0, 1, 0, 1, 0, 1, 99 }, 99);
        }

        public int SingleNumber(int[] nums)
        {
            int ones = 0, twos = 0;
            foreach (int num in nums)
            {
                // Console.WriteLine(string.Empty);
                // Console.WriteLine($"{ones} ^ {num} = {ones ^ num}");
                // Console.WriteLine($"~{twos} = {~twos}");
                // Console.WriteLine($"{ones} ^ {num} & ~{twos} = {ones ^ num & ~twos}");

                // Console.WriteLine($"{twos} ^ {num} = {twos ^ num}");
                // Console.WriteLine($"~{ones} = {~ones}");
                // Console.WriteLine($"{twos} ^ {num} & ~{twos} = {twos ^ num & ~ones}");

                ones = ones ^ num & ~twos;
                twos = twos ^ num & ~ones;

                // Console.WriteLine($"ones = {ones}");
                // Console.WriteLine($"twos = {twos}");
                // Console.WriteLine(string.Empty);
            }
            return ones;
        }
    }
}
